interface Pet {
  id: string;
  name: string;
  type: string;
  level: number;
  experience: number;
  health: number;
  hunger: number;
  happiness: number;
  mood: number;
  skills: PetSkill[];
  lastFed: Date;
  lastPlayed: Date;
  lastTrained: Date;
}

interface PetSkill {
  id: string;
  name: string;
  icon: string;
  description: string;
  level: number;
  experience: number;
}

interface PetState {
  pets: Pet[];
  selectedPetId: string | null;
  foodItems: any[];
  toys: any[];
}

export const pet = {
  namespaced: true,
  
  state: (): PetState => ({
    pets: [],
    selectedPetId: null,
    foodItems: [
      {
        id: 'basic_food',
        name: '普通饲料',
        icon: '🥫',
        hunger: 20,
        price: 10
      },
      {
        id: 'premium_food',
        name: '高级饲料',
        icon: '🍖',
        hunger: 40,
        happiness: 10,
        price: 25
      },
      {
        id: 'special_food',
        name: '特制饲料',
        icon: '🍗',
        hunger: 60,
        happiness: 20,
        health: 10,
        price: 50
      }
    ],
    toys: [
      {
        id: 'ball',
        name: '小球',
        icon: '⚽',
        happiness: 15,
        price: 20
      },
      {
        id: 'bone',
        name: '磨牙骨',
        icon: '🦴',
        happiness: 25,
        health: 5,
        price: 35
      },
      {
        id: 'mouse',
        name: '玩具老鼠',
        icon: '🐭',
        happiness: 30,
        experience: 10,
        price: 45
      }
    ]
  }),

  mutations: {
    addPet(state, pet: Pet) {
      state.pets.push(pet);
    },
    
    updatePet(state, updatedPet: Pet) {
      const index = state.pets.findIndex(p => p.id === updatedPet.id);
      if (index > -1) {
        state.pets[index] = updatedPet;
      }
    },
    
    selectPet(state, petId: string) {
      state.selectedPetId = petId;
    },
    
    updatePetStats(state, { petId, stats }) {
      const pet = state.pets.find(p => p.id === petId);
      if (pet) {
        Object.assign(pet, stats);
      }
    }
  },

  actions: {
    async adoptPet({ commit, state }, { type, name }) {
      const newPet: Pet = {
        id: Date.now().toString(),
        name,
        type,
        level: 1,
        experience: 0,
        health: 100,
        hunger: 100,
        happiness: 100,
        mood: 100,
        skills: [],
        lastFed: new Date(),
        lastPlayed: new Date(),
        lastTrained: new Date()
      };
      
      commit('addPet', newPet);
      return newPet;
    },
    
    async feedPet({ commit, state }, petId: string) {
      const pet = state.pets.find(p => p.id === petId);
      if (!pet) return;
      
      const updatedStats = {
        hunger: Math.min(100, pet.hunger + 20),
        happiness: Math.min(100, pet.happiness + 5),
        lastFed: new Date()
      };
      
      commit('updatePetStats', { petId, stats: updatedStats });
    },
    
    async playWithPet({ commit, state }, petId: string) {
      const pet = state.pets.find(p => p.id === petId);
      if (!pet) return;
      
      const updatedStats = {
        happiness: Math.min(100, pet.happiness + 15),
        hunger: Math.max(0, pet.hunger - 5),
        experience: pet.experience + 10,
        lastPlayed: new Date()
      };
      
      commit('updatePetStats', { petId, stats: updatedStats });
      
      // 检查是否升级
      if (pet.experience >= pet.level * 100) {
        commit('updatePetStats', {
          petId,
          stats: {
            level: pet.level + 1,
            experience: 0,
            health: 100,
            happiness: 100
          }
        });
      }
    },
    
    async trainPet({ commit, state }, petId: string) {
      const pet = state.pets.find(p => p.id === petId);
      if (!pet) return;
      
      // 随机选择一个技能进行训练
      const skillTypes = [
        {
          id: 'harvest',
          name: '收获助手',
          icon: '🌾',
          description: '帮助收获作物，提高收获效率'
        },
        {
          id: 'guard',
          name: '农场守卫',
          icon: '🛡️',
          description: '保护作物免受害虫侵扰'
        },
        {
          id: 'water',
          name: '浇水能手',
          icon: '💧',
          description: '帮助浇水，提高作物生长速度'
        }
      ];
      
      let skill = pet.skills.find(s => s.id === skillTypes[0].id);
      if (!skill) {
        skill = {
          ...skillTypes[0],
          level: 1,
          experience: 0
        };
        pet.skills.push(skill);
      }
      
      const updatedSkill = {
        ...skill,
        experience: skill.experience + 20
      };
      
      if (updatedSkill.experience >= updatedSkill.level * 50) {
        updatedSkill.level += 1;
        updatedSkill.experience = 0;
      }
      
      const updatedPet = {
        ...pet,
        skills: pet.skills.map(s => 
          s.id === updatedSkill.id ? updatedSkill : s
        ),
        happiness: Math.max(0, pet.happiness - 10),
        experience: pet.experience + 15,
        lastTrained: new Date()
      };
      
      commit('updatePet', updatedPet);
    }
  },

  getters: {
    selectedPet: (state) => {
      return state.selectedPetId 
        ? state.pets.find(p => p.id === state.selectedPetId)
        : null;
    },
    
    getPetById: (state) => (id: string) => {
      return state.pets.find(p => p.id === id);
    },
    
    petsByType: (state) => (type: string) => {
      return state.pets.filter(p => p.type === type);
    },
    
    availableFood: (state) => {
      return state.foodItems;
    },
    
    availableToys: (state) => {
      return state.toys;
    }
  }
}; 